//
//  ExperienceViewController.m
//  BTRIP
//
//  Created by johnbk on 15/11/14.
//  Copyright © 2015年 johnbk. All rights reserved.
//

#import "ExperienceViewController.h"
#import "DataHandle.h"
#import "ExperienceModel.h"
#import "ExperienceTableViewCell.h"
#import "ExperienceDetailViewController.h"
@interface ExperienceViewController ()<UITableViewDelegate, UITableViewDataSource>

@end

@implementation ExperienceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
	self.view.backgroundColor = [UIColor whiteColor];
	self.automaticallyAdjustsScrollViewInsets = NO;
	self.array = [NSMutableArray array];
	[self createTableView];
	[self interperateData];

}
- (void)createTableView{
	self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
	[self.view addSubview:self.tableView];
	[self.tableView release];
	self.tableView.delegate = self;
	self.tableView.dataSource = self;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
	ExperienceTableViewCell *cell = [ExperienceTableViewCell tableView:tableView];
	cell.experienceModel = self.array[indexPath.row];
	return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
	return self.array.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
	return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
	return 260;
}
- (void)interperateData{
	NSString *stringFirstHalf = [NSString stringWithFormat:@"http://api.yizhong.cccwei.com/api.php?srv=2022&cid=512665&city_id=%ld&uid=0&tms=20151111221304&sig=e1cab818b7b138d7&wssig=861cd937e14118b8&os_type=1&version=8&coordinate=121.542494", self.selectedCity + 1];
	NSString *stringSecondHalf = @"%2C38.890678&page_size=20&since_id=0";
	[DataHandle contectWithURL:[NSString stringWithFormat:@"%@%@", stringFirstHalf, stringSecondHalf] andParameters:nil andViewController:self andBlock:^(id response) {
		[self.array removeAllObjects];
		NSDictionary *dictionary = response;
		NSArray *array = dictionary[@"data"][@"items"];
		for (NSDictionary *dic in array) {
			ExperienceModel *experienceModel = [[ExperienceModel alloc]init];
			[experienceModel setValuesForKeysWithDictionary:dic];
			[self.array addObject:experienceModel];
			[experienceModel release];
		}
		[self.tableView reloadData];
	}];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)viewWillAppear:(BOOL)animated{
	[self.navigationController.navigationBar setHidden:NO];
	self.selectedCity = [[NSUserDefaults standardUserDefaults]integerForKey:@"selectedCity"];
	[self interperateData];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
	ExperienceDetailViewController *detail = [[ExperienceDetailViewController alloc]init];
	detail.experienceModel = self.array[indexPath.row];
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
	[self.navigationController pushViewController:detail animated:YES];
	[detail release];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
